import CryptoJS from 'crypto-js'
// 加密秘钥
export const aesKey = 'bifuq7eyb04hc81h'
// 加密偏移量
export const aesIv = 'cdkjytgsrj754921'

/** * AES 加密 * @param word 待加密字段 * @param keyStr 加密 key * @returns {string} 返回加密字段 */
export function aesEncrypt(word) {
  const key = CryptoJS.enc.Utf8.parse(aesKey)
  let srcs = ''
  switch (typeof (word)) {
    case 'string':
      srcs = CryptoJS.enc.Utf8.parse(word)
      break
    case 'object':
      srcs = CryptoJS.enc.Utf8.parse(JSON.stringify(word))
      break
    default:
      srcs = CryptoJS.enc.Utf8.parse(word.toString())
  }
  const iv = CryptoJS.enc.Utf8.parse(aesIv)
  const encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 })
  return encrypted.toString()
}
